package com.sduept.nwld.dataserver.timer;

import com.sduept.bigdata.fault.common.ParamDesc;
import com.sduept.core.ParamManager;
import com.sduept.nwld.dataserver.manager.message.MosMessagePushManager;
import com.sduept.permission.entity.PersonnelEntityBean;
import com.sduept.permission.manager.PersonnelManager;
import com.sduept.utils.WebRequestUtil;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class SuperMapServiceCheck {
	private final String receiverParamKey = "map_error_receiver";
	private final String msgContextParamKey = "map_error_msg_context";
	@Autowired
	private ParamManager pc;
	@Autowired
	private MosMessagePushManager mosMessM;
	@Autowired
	private PersonnelManager pm;
	
//	@Schedule(hour = "7", minute = "0", second = "0",persistent=false)
	@Scheduled(cron = "0 0 7 * * ?")
	public void superMapCheckScheduleTimer() {
		String mapURL = pc.get(ParamDesc.MAP_SERVICE_ADDRESS).getValue();
		try {
			WebRequestUtil.getRequest(mapURL, Method.GET,new HashMap<>());
		}catch(java.net.SocketTimeoutException e) {
			String mesgContent = pc.get(msgContextParamKey).getValue();
			try {
				if(StringUtils.isNotEmpty(mesgContent)) {
					mosMessM.sendMsgByMass(getReceiverPhoneList(),"supermap连接异常",mesgContent);
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取接收人
	 * @return
	 */
	private List<String> getReceiverPhoneList(){
		List<String> result = new ArrayList<>();
		String receivers = pc.get(receiverParamKey)==null?null:pc.get(receiverParamKey).getValue();
		if(StringUtils.isEmpty(receivers)) {
			return result;
		}
		String[] rs = receivers.split(",");
		List<PersonnelEntityBean> persons = pm.findPersonnelByCodes(rs);
		for(PersonnelEntityBean pe:persons) {
			result.add(pe.getTelephone());
		}
		return result;
	}
}
